package edu.udlap.spring2008.is300.finalexam.app.mvc.view;

import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.database.objects.ImageRecord;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.model.ModelState;
import edu.udlap.spring2008.is300.finalexam.framework.mvc.view.ViewComponent;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 *
 * @author Ruslán Ledesma Garza
 */
public class ViewToolbarControls extends ViewComponent
        implements ActionListener
{

    protected JPanel mainPanel_ = new JPanel();
    
    protected JButton newButton_ =
            new JButton("<html>" +
                        "   <img src=\"file:imgs/new.png\" align=\"middle\">" +
                        "   Add new image..." +
                        "</html>");
    protected JButton deleteButton_ =
            new JButton("<html>" +
                        "   <img src=\"file:imgs/delete.png\" align=\"middle\">" +
                        "   Delete selected image..." +
                        "</html>");
    protected JButton markFavoriteButton_ =
            new JButton("<html>" +
                        "   <img src=\"file:imgs/favorite.png\" align=\"middle\">" +
                        "   Mark as favorite..." +
                        "</html>");
    protected JButton toggleViewModeButton_ =
            new JButton("<html>" +
                        "   <img src=\"file:imgs/toggle.png\" align=\"middle\">" +
                        "   Toggle view mode..." +
                        "</html>");
    
    public ViewToolbarControls()
    {
        newButton_.addActionListener(this);
        deleteButton_.addActionListener(this);
        markFavoriteButton_.addActionListener(this);
        toggleViewModeButton_.addActionListener(this);
        
        mainPanel_.add(newButton_);
        mainPanel_.add(deleteButton_);
        mainPanel_.add(markFavoriteButton_);
        mainPanel_.add(toggleViewModeButton_);
    }   
    
    // <editor-fold desc="overriden & implemented operations...">
    @Override
    public Component getComponent()
    {
        return (Component)mainPanel_;
    }

    @Override
    public void gridMode()
    {
        ///do nothing...
    }

    @Override
    public void slideShowMode()
    {
        ///do nothing...
    }

    @Override
    public void setInitialState(ModelState modelState)
    {
        selectionChanged(modelState);
        favoritesChanged(modelState);
    }
    
    @Override
    public void imageSetChanged(ModelState modelState)
    {
        ///do nothing...
    }
    
    @Override
    public void selectionChanged(ModelState modelState)
    {
        ImageRecord imageRecord = modelState.getSelectedImageRecord();
        
        ///check whether the currently selected image is favorite or not... 
        if(imageRecord != null)
            markFavoriteButton_.setEnabled(!imageRecord.isFavorite());
        else
            markFavoriteButton_.setEnabled(false);
        
        ///select an status for the delete button...
        deleteButton_.setEnabled(imageRecord != null);
    }

    @Override
    public void favoritesChanged(ModelState modelState)
    {
        ImageRecord imageRecord = modelState.getSelectedImageRecord();
        
        ///check whether the currently selected image is favorite or not... 
        if(imageRecord != null)
            markFavoriteButton_.setEnabled(!imageRecord.isFavorite());
        else
            markFavoriteButton_.setEnabled(false);
    }
    
    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(controller_ == null)
            return;
        
        Object source = e.getSource();
        
        if(source == newButton_)
        {
            controller_.showAddNewDialog();
        }
        else if(source == deleteButton_)
        {
            controller_.deleteSelectedImageRecord();
        }
        else if(source == markFavoriteButton_)
        {
            controller_.toggleFavoriteStatusOfSelectedImageRecord();
        }
        else if(source == toggleViewModeButton_)
        {
            controller_.toggleGridSlideMode();
        }
    }
    // </editor-fold>
}
